const express = require('express');
const router = express.Router();
const userInfoModule = require('../database/module/userInfoModule');
const userModule = require('../database/module/userModule');
const banlanceRecordModule=require('../database/module/balanceRecordModule');
//查找用户信息
router.all('/findInfo',(req,res)=> {
    const data = req.body;
    userInfoModule.findOne({user_id:data.user_id}).then(result1 => {
        if(result1) {
            userModule.findOne({user_id:data.user_id}).then(result2 => {
                let admin_id;
                let mvo_id;
                let bvo_id;
                if(result2.type===0) {
                    admin_id='admin'+data.user_id;
                }
                else if(result2.type===1){
                    mvo_id='mvo'+data.user_id;
                }
                else {
                    bvo_id='bvo'+data.user_id;
                }
                const ret = {
                    user_id: result1.user_id,
                    name: result1.name,
                    description: result1.description,
                    avatar_url: result1.avatar_url,
                    type: result2.type,
                    mvo_id,
                    bvo_id,
                    admin_id
                }
                res.send(ret);
            })
        } else {
            res.send();
        }
    })
})
//修改账户信息
router.all('/editInfo',(req,res)=>{
    const data=req.body;
    userInfoModule.updateOne({user_id:data.user_id},{name:data.name,description:data.description,avatar_url:data.avatar_url}).then(()=>{
        res.send(true);
    }).catch(err=>{
        res.send(false);
    })

})
//修改账户密码
router.all('/editPassword',(req,res)=>{
    const data=req.body;
    userModule.updateOne({user_id:data.user_id},{password:data.password}).then(()=>{
        res.send(true);
    })
})
//查询账户余额
router.all('/checkBalance',(req,res)=>{
    const data=req.body;
    userInfoModule.findOne({user_id:data.user_id}).then(result=>{
        res.send(result);
    }).catch(err=>{
        console.log(err);
    })
})
//查看平台所有余额变化
router.all('/findAllBalanceRecord',(req,res)=>{
    banlanceRecordModule.find().then(result=>{
        res.send(result);
    }).catch(err=>{
        console.log(err);
    })
})
//查看特定用户余额变动
router.all('/findUserBalanceRecord',(req,res)=>{
    const data=req.body;
    banlanceRecordModule.find({user_id:data.user_id}).then(result=>{
        res.send(result);
    }).catch(err=>{
        console.log(err);
    })
})

module.exports = router;